package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {

    Page<DishVO> getPage(DishPageQueryDTO queryDTO);

    @AutoFill(value = OperationType.UPDATE)
    void updateDish(Dish dish);



    @Select("select id, name, category_id, price, image, description, status, create_time, update_time, create_user, update_user  from dish where id = #{id}")
    Dish getDish(Long id);

    @Select("select id,dish_id,name,value  from dish_flavor where dish_id = #{id}")
    List<DishFlavor> getFlavorByDishId(long id);

    void deleteDishByIds(long[] ids);

//    @Delete("delete from dish_flavor where dish_id = #{id}")
    void deleteFlavorByDishIds(long[] ids);

//    @Delete("delete from setmeal_dish where dish_id = #{id}")
//    void deleteSetmealDishByDishId(Long id);

    @Select("select  name from category where id = #{id}")
    String getCategoryNameById(long id);

    @AutoFill(value = OperationType.INSERT)
    //会自动将生成的主键值，赋值给dish对象的id属性
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into dish (name, category_id, price, image, description, status, create_time, update_time, create_user, update_user) " +
            "values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
    void insertDish(Dish dish);

    @Select("select name from setmeal_dish where dish_id = #{id}")
    List<String> getSetmealDishByDishId(Long id);

    void insertDishFlavor(List<DishFlavor> flavors);

    @Select("select id, name, category_id, price, image, description, status, create_time, update_time, create_user, update_user  from dish where category_id = #{categoryId} and status = #{status}")
    List<Dish> getDishByCategoryId(Dish dish);
}
